package com.xm.security.utils;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.util.Objects;

/**
 * 自定义密码处理器
 *
 * @author yousj
 * @version 1.0.0
 * @since 2021/8/18 18:06
 */
@Component
public class MD5PasswordEncoder implements PasswordEncoder {

    @Bean
    @ConditionalOnMissingBean
    public PasswordEncoder passwordEncoder() {
        return new MD5PasswordEncoder();
    }

    @Override
    public String encode(CharSequence charSequence) {
        return DigestUtils.md5DigestAsHex(charSequence.toString().getBytes());
    }

    @Override
    public boolean matches(CharSequence charSequence, String s) {
        return Objects.equals(encode(charSequence), s);
    }
}
